package sri.proiect.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sri.proiect.models.Hotel;
import sri.proiect.search.BasicSearcher;
import sri.proiect.search.MultiCriteriaSearchManager;
import sri.proiect.utils.DBColumn;

public class AdvancedSearchController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		if (request.getParameterMap().size() != 0) 
		{
			String hotel = request.getParameter("hotel");
			String oras = request.getParameter("oras");
			String nrStele = request.getParameter("nrStele");
			String judet = request.getParameter("judet");
			String detalii = request.getParameter("detalii");
			
			MultiCriteriaSearchManager searchManager = MultiCriteriaSearchManager.instance();
			
			testAndAddSearchCriterion(DBColumn.Nume_Hotel, hotel, searchManager);
			testAndAddSearchCriterion(DBColumn.Oras, oras, searchManager);
			testAndAddSearchCriterion(DBColumn.Judet, judet, searchManager);
			testAndAddSearchCriterion(DBColumn.Detalii, detalii, searchManager);
			testAndAddSearchCriterion(DBColumn.NrStele, nrStele, searchManager);
			
			List<Hotel> selectedHotels = searchManager.search();
			
			request.setAttribute("searchResult",selectedHotels);
	    	request.getSession().setAttribute("hotels", selectedHotels);
		}
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("advancedSearch.jsp");
    	dispatcher.forward(request, response);
	}

	private static void testAndAddSearchCriterion(DBColumn column, String value,
			MultiCriteriaSearchManager searchManager) {
		if (value != null && !value.isEmpty()) {
			searchManager.addSearchCriterion(column, value);
		}
	}
	
	public static void main(String[] args) {
		/*MultiCriteriaSearchManager searchManager = MultiCriteriaSearchManager.instance();
		
		testAndAddSearchCriterion(DBColumn.Nume_Hotel, "President", searchManager);
		testAndAddSearchCriterion(DBColumn.Oras, "Bucuresti", searchManager);
		testAndAddSearchCriterion(DBColumn.Judet, null, searchManager);
		testAndAddSearchCriterion(DBColumn.Detalii, null, searchManager);
		
		*/
		BasicSearcher searchManager = BasicSearcher.instance();
		List<Hotel> selectedHotels = searchManager.search("+hotel:President +oras:Bucuresti");
	}
}
